<?php
namespace app\index\controller;
use Endroid\QrCode\QrCode;
use think\Db;
use think\facade\Session;
use think\Request;

class Publics extends Base
{

    //站点2
    public function login(){
    	return view();
    }

    //确认登录
    public function confirm(){
    	if (request()->isPost()) {
    		$post = $_POST;
            if (!isset($post['token']) || empty($post['token'])) {
                return $this->ajaxReturn('错误的授权',1);
            }
            if (!isset($post['username']) || empty($post['username'])) {
                return $this->ajaxReturn('错误的帐号',2);
            }
            $login_qrcode = Db::table('php_login_qrcode')->where(['token'=> $post['token']])->find();
            if (empty($login_qrcode)) {
                return $this->ajaxReturn('授权错误或授权已过期',3);
            }
            $userinfo = Db::table('store_member')->where(['username'=> $post['username']])->find();
            if (empty($userinfo)) {
                return $this->ajaxReturn('帐号错误',4);
            }
            Db::table('php_login_qrcode')->where(['id'=> $login_qrcode['id']])->update(['status'=> 3]);
            return $this->ajaxReturn('登录成功',0,['data'=> ['url'=> url('/','',true,true)]]);
    	}else{
	    	$token = isset($_GET['token']) ? $_GET['token'] : '';
            Session::delete('getrandom');
            $data = [
                'status'        => 1,
                'memberid'      => 1,
                'logintime'     => time(),
                'loginip'       => request()->ip()
            ];
            Db::table('php_login_qrcode')->where(['token'=> $token])->update($data);

	    	$this->assign('token',$token);
	    	return view();    		
    	}
    }

    //二维码生成
    public function qrcode(){
    	//组装签名
    	$time = time();
    	$callbackparame = $this->login_qrcode($time);


        if (isset($callbackparame['status'])) {
            if ($callbackparame['status'] == 1) {
                Db::table('php_login_qrcode')->where(['id'=> $callbackparame['id']])->update(['status'=> 2]);
                return $this->ajaxReturn('已扫码，请确认登录',1,$callbackparame);
            }
            $sign = implode(',', [$callbackparame['rand'],$callbackparame['dqtime']]);
            if ($callbackparame['status'] == 2) {
                return $this->ajaxReturn('等待确认或取消',2);
            }
            if ($callbackparame['status'] == 3) {
                $userinfo = Db::table('store_member')->where(['id'=> $callbackparame['memberid']])->find();
                Session::set('userinfo',$userinfo);
                return $this->ajaxReturn('登录成功',3,['data'=> ['url'=> url('/','',true,true)]]);
            }
        }else{
            $sign = implode(',', [$callbackparame['rand'],$callbackparame['dqtime']]);
        }

    	//生成token
    	$token = _encrypt($sign,'wangxingfeng');
    	$url = url('/index/publics/confirm','',true,true).'?token='.$token;

    	//LOGO地址
    	$logo = WEB_ROOT."/static/computer/img/logo.jpg";
        $qrCode = new QrCode($url);
        //设置前景色
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' =>0, 'a' => 0]);
        //设置背景色
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
        //设置二维码大小
        $qrCode->setSize(200);
        $qrCode->setPadding(20);
        //添加logo
        $qrCode->setLogo($logo);
        //设置logo大小
        $qrCode->setLogoSize(50);
        //$qrCode->setLabel("HelloWorld");
        $qrCode->setLabelFontSize(14);
        $qrCode->setLabelHalign(100);
        //绘制二维码边框
        // $qrCode->setDrawBorder(true);
        //获取二维码数据
        //$img= $qrCode->getDataUri();
        $path = "/uploads/qrcode/{$token}.png";
        $status = Db::table('php_login_qrcode')->where(['rand'=> $callbackparame['rand']])->update(['path'=> $path,'token'=> $token]);
        $imgurl = $qrCode->save(WEB_ROOT.$path);

        if ($status) {
        	return $this->ajaxReturn('二维码过期，已重新生成',0,['data'=> ['url'=> WEB_URL.$path]]);
        }else{
        	return $this->ajaxReturn('二维码无变动',99,['data'=> ['url'=> WEB_URL.$path]]);
        }
    }

    //二维码查询
    private function login_qrcode($time){
    	if (Session::has('getrandom')) {
    		$get_random = Session::get('getrandom');
    		$info = Db::table('php_login_qrcode')->where(['rand'=> $get_random])->field('id,create_time')->order('id desc')->find();
    		if ($time >= $info['create_time']) {
                Db::table('php_login_qrcode')->where(['rand'=> Session::get('getrandom')])->delete();
    			Session::delete('getrandom');
    			$this->login_qrcode($time);
    		}
    		$dqtime = $info['create_time'];
    	}else{
    		$get_random = get_random();
    		$data = [
    			'create_time'=> $time+300,
    			'rand'=> $get_random
    		];
    		Db::table('php_login_qrcode')->insert($data);
    		Session::set('getrandom',$get_random);
    		$dqtime = $time;
    	}
    	$info = Db::table('php_login_qrcode')->where(['rand'=> $get_random])->order('id desc')->find();
    	$info['dqtime'] = $dqtime;
    	return $info;
    }
}